package com.kk.base.service.home;

import com.kk.base.bean.PageBean;
import com.kk.base.dao.home.ComplaintDao;
import com.kk.base.entity.home.Complaint;
import com.kk.base.entity.home.HomeUser;
import com.kk.base.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

/**
 * @info :  投诉数据库处理层
 */
@Service
public class ComplaintService{


    @Autowired
    private ComplaintDao complaintDao;


    /**
     * 根据投诉id查询
     * @param id
     * @return
     */
    public Complaint find(Long id){
        return complaintDao.find(id);
    }

    /**
     * 根据投诉编号和投诉用户查询
     * @param waybillNumber
     * @return
     */
    public Complaint findByHomeUser_IdAndWaybillNumber(String waybillNumber){
        HomeUser homeUser = SessionUtil.getHomeUser();
        return complaintDao.findByHomeUser_IdAndWaybillNumber(homeUser.getId(),waybillNumber);
    }



    /**
     * 投诉添加/编辑操作
     * @param complaint
     * @return
     */
    public Complaint save(Complaint complaint){
        return complaintDao.save(complaint);
    }


    /**
     * 分页查询
     * @param complaint
     * @param pageBean
     * @return
     */
    public PageBean<Complaint> findList(Complaint complaint,PageBean<Complaint> pageBean){
        ExampleMatcher withMatcher = ExampleMatcher.matching().withMatcher("waybillNumber",
                ExampleMatcher.GenericPropertyMatchers.contains());
        withMatcher = withMatcher.withIgnorePaths("status");
        Example<Complaint> example = Example.of(complaint, withMatcher);
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageBean.getCurrentPage()-1, pageBean.getPageSize(),sort);
        Page<Complaint> findAll = complaintDao.findAll(example, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }




    /**
     * 按照投诉id删除
     * @param id
     */
    public void delete(Long id){
        complaintDao.deleteById(id);
    }

 
    


}
